package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Account;

@Repository
public class AccountDaoImpl extends BasicDaoImpl<Account>{
	@Autowired 
	private SessionFactory sessionFactory;	
	
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByEmpId(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account where accountCreator.id = :id and isDeleted = false") ;
		query.setParameter("id", id);
		List<Account> accounts = new ArrayList<Account>();
		accounts = query.list();
		return accounts;
		
	}
	
	@SuppressWarnings("unchecked")
	public Account getAccountByName(String accountName) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account where accountName=  :accountName");
		query.setParameter("accountName", accountName);
		List<Account>list = new ArrayList<Account>();
		list = query.list();
		if(list.size()>0){
			return list.get(0);
			
		}else{
			return null;
		}
	
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountForCompany(Integer companyName) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account acc where isDeleted= :isDeleted and acc.accountCreator.id = :companyName ");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts;
	}
	
	@SuppressWarnings("unchecked")
	public Integer getAllAccountForCompanySize(Integer companyName) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account acc where isDeleted= :isDeleted and acc.accountCreator.id = :companyName ");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts.size();

	}
}
